package com.anan.builder;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class BuildResources {
    public static void create(String packageName,String dataBaseName){
        createResources(packageName,dataBaseName);
        createLogbackSpring(packageName);
        createMainClass(packageName);
        createInitRun(packageName);
        createPom(packageName);
    }
    private static void createResources(String projectName,String dataBaseName) {
        String path = System.getProperty("user.dir") + "/" + projectName + "/src/main/resources";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, "application.properties");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("# 应用服务 WEB 访问端口\n" +
                    "server.port=8081\n" +
                    "#session过期时间 60M 一个小时\n" +
                    "server.servlet.session.timeout=PT60M\n" +
                    "#处理favicon\n" +
                    "spring.mvc.favicon.enable=false\n" +
                    "spring.servlet.multipart.max-file-size=15MB\n" +
                    "spring.servlet.multipart.max-request-size=15MB\n" +
                    "#错误页处理\n" +
                    "spring.mvc.throw-exception-if-no-handler-found=true\n" +
                    "spring.web.resources.add-mappings=false\n" +
                    "#数据库配置\n" +
                    "spring.datasource.url=jdbc:mysql://127.0.0.1:3306/"+dataBaseName+"?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false\n" +
                    "spring.datasource.username=root\n" +
                    "spring.datasource.password=root\n" +
                    "spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver\n" +
                    "spring.datasource.hikari.pool-name=HikariCPDatasource\n" +
                    "spring.datasource.hikari.minimum-idle=5\n" +
                    "spring.datasource.hikari.idle-timeout=180000\n" +
                    "spring.datasource.hikari.maximum-pool-size=10\n" +
                    "spring.datasource.hikari.auto-commit=true\n" +
                    "spring.datasource.hikari.max-lifetime=1800000\n" +
                    "spring.datasource.hikari.connection-timeout=30000\n" +
                    "spring.datasource.hikari.connection-test-query=SELECT 1\n\n" +
                    "mybatis.mapper-locations=classpath:mapper/*.xml\n" +
                    "spring.thymeleaf.prefix=classpath:/templates/\n" +
                    "spring.thymeleaf.check-template-location=true\n" +
                    "spring.thymeleaf.cache=true\n" +
                    "spring.thymeleaf.suffix=.html\n" +
                    "spring.thymeleaf.encoding=UTF-8\n" +
                    "spring.thymeleaf.mode=HTML5\n\n"+
                    "#文件输出路径\n"+
                    "path.resources=D:/anan/\n"+
                    "prefix.path=static\n"+
                    "#发送邮件配置相关\n" +
                    "# 配置邮件服务器的地址 smtp.qq.com\n" +
                    "spring.mail.host=smtp.qq.com\n" +
                    "# 配置邮件服务器的端口（465或587）\n" +
                    "spring.mail.port=465\n" +
                    "# 配置用户的账号\n" +
                    "spring.mail.username=2328791101@qq.com\n" +
                    "# 配置用户的密码\n" +
                    "spring.mail.password=sjvtcqzfdnlrdjdh\n" +
                    "# 配置默认编码\n" +
                    "spring.mail.default-encoding=UTF-8\n" +
                    "# SSL 连接配置\n" +
                    "spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory\n" +
                    "# 开启 debug，这样方便开发者查看邮件发送日志\n" +
                    "spring.mail.properties.mail.debug=true\n" +
                    "#邮件配置结束\n" +
                    "#Spring redis配置\n" +
                    "# Redis数据库索引（默认为0）\n" +
                    "spring.redis.database=0\n" +
                    "spring.redis.host=127.0.0.1\n" +
                    "spring.redis.port=6379\n" +
                    "# 连接池最大连接数（使用负值表示没有限制）\n" +
                    "spring.redis.jedis.pool.max-active=20\n" +
                    "# 连接池最大阻塞等待时间（使用负值表示没有限制）\n" +
                    "spring.redis.jedis.pool.max-wait=-1\n" +
                    "# 连接池中的最大空闲连接\n" +
                    "spring.redis.jedis.pool.max-idle=10\n" +
                    "# 连接池中的最小空闲连接\n" +
                    "spring.redis.jedis.pool.min-idle=0\n" +
                    "# 连接超时时间（毫秒）\n" +
                    "spring.redis.timeout=2000\n" +
                    "#日志级别配置\n" +
                    "log.root.level=debug\n" +
                    "#超级管理员id\n" +
                    "admin.emails=root@qq.com\n" +
                    "\n" +
                    "project.folder=\n" +
                    "url = http://127.0.0.1:8081\n" +
                    "#是否是开发环境\n" +
                    "dev=false\n" +
                    "##qq登陆相关##\n" +
                    "qq.app.id=102054903\n" +
                    "qq.app.key=Wt2wb2EArLaO0NVP\n" +
                    "qq.url.authorization=https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=%s&redirect_uri=%s&state=%s\n" +
                    "qq.url.access.token=https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=%s&client_secret=%s&code=%s&redirect_uri=%s\n" +
                    "qq.url.openid=https://graph.qq.com/oauth2.0/me?access_token=%s\n" +
                    "qq.url.user.info=https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s\n" +
                    "qq.url.redirect=http://www.xiaochenbz.com/qqLogin\n" +
                    "\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    private static void createLogbackSpring(String projectName) {
        String path = System.getProperty("user.dir") + "/" + projectName + "/src/main/resources";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, "logback-spring.xml");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<!-- 日志级别从低到高分为TRACE < DEBUG < INFO < WARN < ERROR < FATAL，比如: 如果设置为WARN，则低于WARN的信息都不会输出 -->\n" +
                    "<!-- scan:当此属性设置为true时，配置文档如果发生改变，将会被重新加载，默认值为true -->\n" +
                    "<!-- scanPeriod:设置监测配置文档是否有修改的时间间隔，如果没有给出时间单位，默认单位是毫秒。当scan为true时，此属性生效。默认的时间间隔为1分钟。 -->\n" +
                    "<!-- debug:当此属性设置为true时，将打印出logback内部日志信息，实时查看logback运行状态。默认值为false。 -->\n" +
                    "<configuration  scan=\"true\" scanPeriod=\"10 seconds\">\n" +
                    "    <contextName>logback</contextName>\n" +
                    "\n" +
                    "    <!-- name的值是变量的名称，value的值时变量定义的值。通过定义的值会被插入到logger上下文中。定义后，可以使“${}”来使用变量。 -->\n" +
                    "    <springProperty scope=\"context\" name=\"myLogLocation\" source=\"logging.file-location\" default=\"/var/log/myapp\"/>\n" +
                    "    <property name=\"log.path\" value=\"logs\"/>\n" +
                    "\n" +
                    "    <!--0. 日志格式和颜色渲染 -->\n" +
                    "    <!-- 彩色日志依赖的渲染类 -->\n" +
                    "    <conversionRule conversionWord=\"clr\" converterClass=\"org.springframework.boot.logging.logback.ColorConverter\" />\n" +
                    "    <conversionRule conversionWord=\"wex\" converterClass=\"org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter\" />\n" +
                    "    <conversionRule conversionWord=\"wEx\" converterClass=\"org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter\" />\n" +
                    "    <!-- 彩色日志格式 -->\n" +
                    "    <property name=\"CONSOLE_LOG_PATTERN\" value=\"${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}\"/>\n" +
                    "\n" +
                    "    <!--1. 输出到控制台-->\n" +
                    "    <appender name=\"CONSOLE\" class=\"ch.qos.logback.core.ConsoleAppender\">\n" +
                    "        <!--此日志appender是为开发使用，只配置最底级别，控制台输出的日志级别是大于或等于此级别的日志信息-->\n" +
                    "        <filter class=\"ch.qos.logback.classic.filter.ThresholdFilter\">\n" +
                    "            <level>debug</level>\n" +
                    "        </filter>\n" +
                    "        <encoder>\n" +
                    "            <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>\n" +
                    "            <!-- 设置字符集 -->\n" +
                    "            <charset>UTF-8</charset>\n" +
                    "        </encoder>\n" +
                    "    </appender>\n" +
                    "\n" +
                    "    <!--2. 输出到文档-->\n" +
                    "    <!-- 2.1 level为 DEBUG 日志，时间滚动输出  -->\n" +
                    "    <appender name=\"DEBUG_FILE\" class=\"ch.qos.logback.core.rolling.RollingFileAppender\">\n" +
                    "        <!-- 正在记录的日志文档的路径及文档名 -->\n" +
                    "        <file>${log.path}/debug.log</file>\n" +
                    "        <!--日志文档输出格式-->\n" +
                    "        <encoder>\n" +
                    "            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>\n" +
                    "            <charset>UTF-8</charset> <!-- 设置字符集 -->\n" +
                    "        </encoder>\n" +
                    "        <!-- 日志记录器的滚动策略，按日期，按大小记录 -->\n" +
                    "        <rollingPolicy class=\"ch.qos.logback.core.rolling.TimeBasedRollingPolicy\">\n" +
                    "            <!-- 日志归档 -->\n" +
                    "            <fileNamePattern>${log.path}/debug-%d{yyyy-MM-dd}.%i.log</fileNamePattern>\n" +
                    "            <timeBasedFileNamingAndTriggeringPolicy class=\"ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP\">\n" +
                    "                <maxFileSize>100MB</maxFileSize>\n" +
                    "            </timeBasedFileNamingAndTriggeringPolicy>\n" +
                    "            <!--日志文档保留天数-->\n" +
                    "            <maxHistory>15</maxHistory>\n" +
                    "        </rollingPolicy>\n" +
                    "        <!-- 此日志文档只记录debug级别的 -->\n" +
                    "        <filter class=\"ch.qos.logback.classic.filter.LevelFilter\">\n" +
                    "            <level>debug</level>\n" +
                    "            <onMatch>ACCEPT</onMatch>\n" +
                    "            <onMismatch>DENY</onMismatch>\n" +
                    "        </filter>\n" +
                    "    </appender>\n" +
                    "\n" +
                    "    <!-- 2.2 level为 INFO 日志，时间滚动输出  -->\n" +
                    "    <appender name=\"INFO_FILE\" class=\"ch.qos.logback.core.rolling.RollingFileAppender\">\n" +
                    "        <!-- 正在记录的日志文档的路径及文档名 -->\n" +
                    "        <file>${log.path}/info.log</file>\n" +
                    "        <!--日志文档输出格式-->\n" +
                    "        <encoder>\n" +
                    "            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>\n" +
                    "            <charset>UTF-8</charset>\n" +
                    "        </encoder>\n" +
                    "        <!-- 日志记录器的滚动策略，按日期，按大小记录 -->\n" +
                    "        <rollingPolicy class=\"ch.qos.logback.core.rolling.TimeBasedRollingPolicy\">\n" +
                    "            <!-- 每天日志归档路径以及格式 -->\n" +
                    "            <fileNamePattern>${log.path}/info-%d{yyyy-MM-dd}.%i.log</fileNamePattern>\n" +
                    "            <timeBasedFileNamingAndTriggeringPolicy class=\"ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP\">\n" +
                    "                <maxFileSize>100MB</maxFileSize>\n" +
                    "            </timeBasedFileNamingAndTriggeringPolicy>\n" +
                    "            <!--日志文档保留天数-->\n" +
                    "            <maxHistory>15</maxHistory>\n" +
                    "        </rollingPolicy>\n" +
                    "        <!-- 此日志文档只记录info级别的 -->\n" +
                    "        <filter class=\"ch.qos.logback.classic.filter.LevelFilter\">\n" +
                    "            <level>info</level>\n" +
                    "            <onMatch>ACCEPT</onMatch>\n" +
                    "            <onMismatch>DENY</onMismatch>\n" +
                    "        </filter>\n" +
                    "    </appender>\n" +
                    "\n" +
                    "    <!-- 2.3 level为 WARN 日志，时间滚动输出  -->\n" +
                    "    <appender name=\"WARN_FILE\" class=\"ch.qos.logback.core.rolling.RollingFileAppender\">\n" +
                    "        <!-- 正在记录的日志文档的路径及文档名 -->\n" +
                    "        <file>${log.path}/warn.log</file>\n" +
                    "        <!--日志文档输出格式-->\n" +
                    "        <encoder>\n" +
                    "            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>\n" +
                    "            <charset>UTF-8</charset> <!-- 此处设置字符集 -->\n" +
                    "        </encoder>\n" +
                    "        <!-- 日志记录器的滚动策略，按日期，按大小记录 -->\n" +
                    "        <rollingPolicy class=\"ch.qos.logback.core.rolling.TimeBasedRollingPolicy\">\n" +
                    "            <fileNamePattern>${log.path}/warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern>\n" +
                    "            <timeBasedFileNamingAndTriggeringPolicy class=\"ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP\">\n" +
                    "                <maxFileSize>100MB</maxFileSize>\n" +
                    "            </timeBasedFileNamingAndTriggeringPolicy>\n" +
                    "            <!--日志文档保留天数-->\n" +
                    "            <maxHistory>15</maxHistory>\n" +
                    "        </rollingPolicy>\n" +
                    "        <!-- 此日志文档只记录warn级别的 -->\n" +
                    "        <filter class=\"ch.qos.logback.classic.filter.LevelFilter\">\n" +
                    "            <level>warn</level>\n" +
                    "            <onMatch>ACCEPT</onMatch>\n" +
                    "            <onMismatch>DENY</onMismatch>\n" +
                    "        </filter>\n" +
                    "    </appender>\n" +
                    "\n" +
                    "    <!-- 2.4 level为 ERROR 日志，时间滚动输出  -->\n" +
                    "    <appender name=\"ERROR_FILE\" class=\"ch.qos.logback.core.rolling.RollingFileAppender\">\n" +
                    "        <!-- 正在记录的日志文档的路径及文档名 -->\n" +
                    "        <file>${log.path}/error.log</file>\n" +
                    "        <!--日志文档输出格式-->\n" +
                    "        <encoder>\n" +
                    "            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>\n" +
                    "            <charset>UTF-8</charset> <!-- 此处设置字符集 -->\n" +
                    "        </encoder>\n" +
                    "        <!-- 日志记录器的滚动策略，按日期，按大小记录 -->\n" +
                    "        <rollingPolicy class=\"ch.qos.logback.core.rolling.TimeBasedRollingPolicy\">\n" +
                    "            <fileNamePattern>${log.path}/error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>\n" +
                    "            <timeBasedFileNamingAndTriggeringPolicy class=\"ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP\">\n" +
                    "                <maxFileSize>100MB</maxFileSize>\n" +
                    "            </timeBasedFileNamingAndTriggeringPolicy>\n" +
                    "            <!--日志文档保留天数-->\n" +
                    "            <maxHistory>15</maxHistory>\n" +
                    "        </rollingPolicy>\n" +
                    "        <!-- 此日志文档只记录ERROR级别的 -->\n" +
                    "        <filter class=\"ch.qos.logback.classic.filter.LevelFilter\">\n" +
                    "            <level>ERROR</level>\n" +
                    "            <onMatch>ACCEPT</onMatch>\n" +
                    "            <onMismatch>DENY</onMismatch>\n" +
                    "        </filter>\n" +
                    "    </appender>\n" +
                    "\n" +
                    "    <!-- 2.5 所有 除了DEBUG级别的其它高于DEBUG的 日志，记录到一个文件  -->\n" +
                    "    <appender name=\"ALL_FILE\" class=\"ch.qos.logback.core.rolling.RollingFileAppender\">\n" +
                    "        <!-- 正在记录的日志文档的路径及文档名 -->\n" +
                    "        <file>${log.path}/all.log</file>\n" +
                    "        <!--日志文档输出格式-->\n" +
                    "        <encoder>\n" +
                    "            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>\n" +
                    "            <charset>UTF-8</charset> <!-- 此处设置字符集 -->\n" +
                    "        </encoder>\n" +
                    "        <!-- 日志记录器的滚动策略，按日期，按大小记录 -->\n" +
                    "        <rollingPolicy class=\"ch.qos.logback.core.rolling.TimeBasedRollingPolicy\">\n" +
                    "            <fileNamePattern>${log.path}/all-%d{yyyy-MM-dd}.%i.log</fileNamePattern>\n" +
                    "            <timeBasedFileNamingAndTriggeringPolicy class=\"ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP\">\n" +
                    "                <maxFileSize>100MB</maxFileSize>\n" +
                    "            </timeBasedFileNamingAndTriggeringPolicy>\n" +
                    "            <!--日志文档保留天数-->\n" +
                    "            <maxHistory>15</maxHistory>\n" +
                    "        </rollingPolicy>\n" +
                    "        <!-- 此日志文档记录除了DEBUG级别的其它高于DEBUG的 -->\n" +
                    "        <filter class=\"ch.qos.logback.classic.filter.LevelFilter\">\n" +
                    "            <level>DEBUG</level>\n" +
                    "            <onMatch>DENY</onMatch>\n" +
                    "            <onMismatch>ACCEPT</onMismatch>\n" +
                    "        </filter>\n" +
                    "    </appender>\n" +
                    "\n" +
                    "    <!--\n" +
                    "        <logger>用来设置某一个包或者具体的某一个类的日志打印级别、\n" +
                    "        以及指定<appender>。<logger>仅有一个name属性，\n" +
                    "        一个可选的level和一个可选的addtivity属性。\n" +
                    "        name:用来指定受此logger约束的某一个包或者具体的某一个类。\n" +
                    "        level:用来设置打印级别，大小写无关：TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF，\n" +
                    "              还有一个特殊值INHERITED或者同义词NULL，代表强制执行上级的级别。\n" +
                    "              如果未设置此属性，那么当前logger将会继承上级的级别。\n" +
                    "        addtivity:是否向上级logger传递打印信息。默认是true。\n" +
                    "        <logger name=\"org.springframework.web\" level=\"info\"/>\n" +
                    "        <logger name=\"org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor\" level=\"INFO\"/>\n" +
                    "    -->\n" +
                    "\n" +
                    "    <!--\n" +
                    "        使用mybatis的时候，sql语句是debug下才会打印，而这里我们只配置了info，所以想要查看sql语句的话，有以下两种操作：\n" +
                    "        第一种把<root level=\"info\">改成<root level=\"DEBUG\">这样就会打印sql，不过这样日志那边会出现很多其他消息\n" +
                    "        第二种就是单独给dao下目录配置debug模式，代码如下，这样配置sql语句会打印，其他还是正常info级别：\n" +
                    "        【logging.level.org.mybatis=debug logging.level.dao=debug】\n" +
                    "     -->\n" +
                    "\n" +
                    "    <!--\n" +
                    "        root节点是必选节点，用来指定最基础的日志输出级别，只有一个level属性\n" +
                    "        level:用来设置打印级别，大小写无关：TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF，\n" +
                    "        不能设置为INHERITED或者同义词NULL。默认是DEBUG\n" +
                    "        可以包含零个或多个元素，标识这个appender将会添加到这个logger。\n" +
                    "    -->\n" +
                    "\n" +
                    "    <!-- 4  最终的策略：\n" +
                    "                 基本策略(root级) + 根据profile在启动时, logger标签中定制化package日志级别(优先级高于上面的root级)-->\n" +
                    "\n" +
                    "        <root level=\"INFO,ERROR,DEBUG\">\n" +
                    "            <appender-ref ref=\"CONSOLE\" />\n" +
                    "            <appender-ref ref=\"DEBUG_FILE\" />\n" +
                    "            <appender-ref ref=\"INFO_FILE\" />\n" +
                    "            <appender-ref ref=\"WARN_FILE\" />\n" +
                    "            <appender-ref ref=\"ERROR_FILE\" />\n" +
                    "            <appender-ref ref=\"ALL_FILE\" />\n" +
                    "        </root>\n" +
                    "\n" +
                    "\n" +
                    "</configuration>\n" +
                    "\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    private static void createMainClass(String projectName) {
        String path = System.getProperty("user.dir") + "/" + projectName + "/src/main/java/com/anan";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, "Application.java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan;\n" +
                    "\n" +
                    "import org.mybatis.spring.annotation.MapperScan;\n" +
                    "import org.springframework.boot.SpringApplication;\n" +
                    "import org.springframework.boot.autoconfigure.SpringBootApplication;\n" +
                    "import org.springframework.scheduling.annotation.EnableAsync;\n" +
                    "import org.springframework.scheduling.annotation.EnableScheduling;\n" +
                    "import org.springframework.transaction.annotation.EnableTransactionManagement;\n" +
                    "\n" +
                    "@EnableAsync\n" +
                    "@SpringBootApplication\n" +
                    "@MapperScan({\"com.anan.mapper\"})\n" +
                    "@EnableTransactionManagement\n" +
                    "@EnableScheduling\n" +
                    "public class Application {\n" +
                    "\n" +
                    "    public static void main(String[] args) {\n" +
                    "        SpringApplication.run(Application.class, args);\n" +
                    "    }\n" +
                    "\n" +
                    "}\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    private static void createInitRun(String projectName) {
        String path = System.getProperty("user.dir") + "/" + projectName + "/src/main/java/com/anan";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, "InitRun.java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan;\n" +
                    "\n" +
                    "import com.anan.exception.BusinessException;\n" +
                    "import com.anan.utils.RedisUtils;\n" +
                    "import org.slf4j.Logger;\n" +
                    "import org.slf4j.LoggerFactory;\n" +
                    "import org.springframework.boot.ApplicationArguments;\n" +
                    "import org.springframework.boot.ApplicationRunner;\n" +
                    "import org.springframework.stereotype.Component;\n" +
                    "\n" +
                    "import javax.annotation.Resource;\n" +
                    "import javax.sql.DataSource;\n" +
                    "import java.io.File;\n" +
                    "\n" +
                    "@Component(\"initRun\")\n" +
                    "public class InitRun implements ApplicationRunner {\n" +
                    "\n" +
                    "    private static final Logger logger = LoggerFactory.getLogger(InitRun.class);\n" +
                    "\n" +
                    "    @Resource\n" +
                    "    private DataSource dataSource;\n" +
                    "\n" +
                    "    @Resource\n" +
                    "    private RedisUtils redisUtils;\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public void run(ApplicationArguments args) {  // 在springboot 启动类启动以后执行的方法 通常进行初始化操作\n" +
                    "        try {\n" +
                    "            dataSource.getConnection();  // 用于获取数据库连接对象。 测试是否可以获取链接对象\n" +
                    "        } catch (Exception e) {\n" +
                    "            logger.error(\"数据库连接失败，请检查配置\");\n" +
                    "            throw new BusinessException(\"服务器启动失败  数据库连接失败 请检查配置\");\n" +
                    "        }\n" +
                    "        try {\n" +
                    "            redisUtils.get(\"\");  // 测试redis是否开启\n" +
                    "        } catch (Exception e) {\n" +
                    "            logger.error(\"redis设置失败，请检查配置\");\n" +
                    "            throw new BusinessException(\"服务器启动失败  redis设置失败 请检查配置 是否启动redis服务器\");\n" +
                    "        }\n" +
                    "        logger.info(\"服务器启动成功!\");\n" +
                    "\n" +
                    "\n" +
                    "    }\n" +
                    "\n" +
                    "/*这是一个名为 InitRun 的类，使用了 @Component 注解表示它是一个 Spring 组件，并实现了 ApplicationRunner 接口，表示在 Spring 应用程序启动完成后要执行的初始化操作。\n" +
                    "\n" +
                    "在这个类中，使用了 @Resource 注解注入了两个依赖：DataSource 和 RedisComponent，这些依赖在 Spring 容器中被创建和管理。DataSource 对象表示一个数据库连接池，用于管理连接到数据库的连接；RedisComponent 对象表示一个 Redis 客户端，用于操作 Redis 数据库。\n" +
                    "\n" +
                    "在 InitRun 类中，实现了 ApplicationRunner 接口的 run() 方法，这个方法在 Spring 应用程序启动完成后会被执行。在这个方法中，可以编写一些初始化操作，例如连接数据库、初始化数据、启动定时任务等等。这些操作可以使用注入的 DataSource 和 RedisComponent 对象进行。在执行完初始化操作后，应用程序就可以正常运行了。*/\n" +
                    "}\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    private static void createPom(String projectName) {
        String path = System.getProperty("user.dir") + "/" + projectName + "/";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, "pom.xml");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                    "         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
                    "    <parent>\n" +
                    "        <groupId>org.springframework.boot</groupId>\n" +
                    "        <artifactId>spring-boot-starter-parent</artifactId>\n" +
                    "        <version>2.2.2.RELEASE</version>\n" +
                    "        <relativePath/>\n" +
                    "    </parent>\n" +
                    "    <modelVersion>4.0.0</modelVersion>\n" +
                    "\n" +
                    "    <groupId>com.anan</groupId>\n" +
                    "    <artifactId>"+projectName+"</artifactId>\n" +
                    "    <version>1.0</version>\n" +
                    "    <packaging>jar</packaging>\n" +
                    "    <name>"+projectName+"</name>\n" +
                    "    <description>server</description>\n" +
                    "\n" +
                    "    <properties>\n" +
                    "        <java.version>11</java.version>\n" +
                    "        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n" +
                    "        <maven.compiler.source>11</maven.compiler.source>\n" +
                    "        <maven.compiler.target>11</maven.compiler.target>\n" +
                    "        <skipTests>true</skipTests>\n" +
                    "\n" +
                    "    </properties>\n" +
                    "\n" +
                    "    <dependencies>\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.springframework.boot</groupId>\n" +
                    "            <artifactId>spring-boot-starter-web</artifactId>\n" +
                    "            <exclusions>\n" +
                    "                <exclusion>\n" +
                    "                    <groupId>ch.qos.logback</groupId>\n" +
                    "                    <artifactId>logback-classic</artifactId>\n" +
                    "                </exclusion>\n" +
                    "                <exclusion>\n" +
                    "                    <groupId>ch.qos.logback</groupId>\n" +
                    "                    <artifactId>logback-core</artifactId>\n" +
                    "                </exclusion>\n" +
                    "            </exclusions>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!--邮件发送-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.springframework.boot</groupId>\n" +
                    "            <artifactId>spring-boot-starter-mail</artifactId>\n" +
                    "            <version>2.6.1</version>\n" +
                    "        </dependency>\n" +
                    "        \n" +
                    "        <!-- hutool 邮件发送依赖 -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>com.sun.mail</groupId>\n" +
                    "            <artifactId>javax.mail</artifactId>\n" +
                    "            <version>1.6.2</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "\n" +
                    "        <!--redis -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.springframework.boot</groupId>\n" +
                    "            <artifactId>spring-boot-starter-data-redis</artifactId>\n" +
                    "            <version>2.6.1</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!--mybatis-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.mybatis.spring.boot</groupId>\n" +
                    "            <artifactId>mybatis-spring-boot-starter</artifactId>\n" +
                    "            <version>1.3.2</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "\n" +
                    "        <!-- 数据库-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>mysql</groupId>\n" +
                    "            <artifactId>mysql-connector-java</artifactId>\n" +
                    "            <version>8.0.23</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "\n" +
                    "        <!-- 日志版本 -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>ch.qos.logback</groupId>\n" +
                    "            <artifactId>logback-classic</artifactId>\n" +
                    "            <version>1.2.10</version>\n" +
                    "        </dependency>\n" +
                    "        <dependency>\n" +
                    "            <groupId>ch.qos.logback</groupId>\n" +
                    "            <artifactId>logback-core</artifactId>\n" +
                    "            <version>1.2.10</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!--切面 AOP-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.aspectj</groupId>\n" +
                    "            <artifactId>aspectjweaver</artifactId>\n" +
                    "            <version>1.9.4</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!--fastjson-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>com.alibaba</groupId>\n" +
                    "            <artifactId>fastjson</artifactId>\n" +
                    "            <version>1.2.75</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!--apache common-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.apache.commons</groupId>\n" +
                    "            <artifactId>commons-lang3</artifactId>\n" +
                    "            <version>3.4</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!-- MD5 加密 -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>commons-codec</groupId>\n" +
                    "            <artifactId>commons-codec</artifactId>\n" +
                    "            <version>1.9</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <dependency>\n" +
                    "            <groupId>commons-io</groupId>\n" +
                    "            <artifactId>commons-io</artifactId>\n" +
                    "            <version>2.5</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!-- https://doc.hutool.cn/pages/index/ -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>cn.hutool</groupId>\n" +
                    "            <artifactId>hutool-all</artifactId>\n" +
                    "            <version>5.8.16</version>\n" +
                    "        </dependency>\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.apache.poi</groupId>\n" +
                    "            <artifactId>poi-ooxml</artifactId>\n" +
                    "            <version>5.0.0</version>\n" +
                    "        </dependency>\n" +
                    "        <dependency>\n" +
                    "            <groupId>xerces</groupId>\n" +
                    "            <artifactId>xercesImpl</artifactId>\n" +
                    "            <version>2.12.0</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "<!--        # 版本在maven生效需要时间，如获取不到对应的版本，可以调低版本号-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>com.tencentcloudapi</groupId>\n" +
                    "            <artifactId>tencentcloud-sdk-java-common</artifactId>\n" +
                    "            <version>3.1.756</version>\n" +
                    "        </dependency>\n" +
                    "        <dependency>\n" +
                    "            <groupId>com.tencentcloudapi</groupId>\n" +
                    "            <artifactId>tencentcloud-sdk-java-sms</artifactId>\n" +
                    "            <version>3.1.758</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.apache.httpcomponents</groupId>\n" +
                    "            <artifactId>httpclient</artifactId>\n" +
                    "            <version>4.5</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.projectlombok</groupId>\n" +
                    "            <artifactId>lombok</artifactId>\n" +
                    "            <optional>true</optional>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!--thymeleaf的依赖-->\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.springframework.boot</groupId>\n" +
                    "            <artifactId>spring-boot-starter-thymeleaf</artifactId>\n" +
                    "            <version>2.7.5</version>\n" +
                    "        </dependency>\n" +
                    "    </dependencies>\n"+
                    "\n" +
                    "    <build>\n" +
                    "        <plugins>\n" +
                    "            <plugin>\n" +
                    "                <groupId>org.springframework.boot</groupId>\n" +
                    "                <artifactId>spring-boot-maven-plugin</artifactId>\n" +
                    "                <version>2.2.2.RELEASE</version>\n" +
                    "                <executions>\n" +
                    "                    <execution>\n" +
                    "                        <goals>\n" +
                    "                            <goal>\n" +
                    "                                repackage\n" +
                    "                            </goal>\n" +
                    "                        </goals>\n" +
                    "                    </execution>\n" +
                    "                </executions>\n" +
                    "                <configuration>\n" +
                    "                    <mainClass>com.anan.Application</mainClass>\n" +
                    "                </configuration>\n" +
                    "            </plugin>\n" +
                    "        </plugins>\n" +
                    "    </build>\n" +
                    "</project>\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
